//
// Created by dddgzc on 2025/1/23.
//

#include "../include/GameScene.h"

#include "Content.h"
#include "PlayerSelectionScene.h"

void GameScene::onEnter() {
    isGameOver = false;
    isSolidOutStarted = false;

    // 计算游戏结算标题的位置
    winnerBarPos.x = -1282;
    winnerBarPos.y = (Content::WINDOW_HEIGHT - 209) / 2;
    winnerBarPosDst.x = (Content::WINDOW_WIDTH - 1282) / 2;
    winnerBarPosDst.y = winnerBarPos.y;

    winnerBarTextPos.x = winnerBarPos.x;
    winnerBarTextPos.y = winnerBarPos.y + (209 - 215) / 2;
    winnerBarTextPosDst.y = winnerBarTextPos.y;
    winnerBarTextPosDst.x = (Content::WINDOW_WIDTH - 646) / 2;

    winnerBarSolidInTimer.resetTime();
    winnerBarSolidInTimer.setWaitTime(2500);
    winnerBarSolidInTimer.setIsOneShot(true);
    winnerBarSolidInTimer.setCallback([&]() {
        isSolidOutStarted = true;
    });

    winnerBarSolidOutTimer.resetTime();
    winnerBarSolidOutTimer.setWaitTime(1000);
    winnerBarSolidOutTimer.setIsOneShot(true);
    winnerBarSolidOutTimer.setCallback([&]() {
        // 返回选择角色的界面
        sceneManager->switchTo(SceneManager::SceneType::PlayerSelectionScene);
    });

    player01->setPosition(200, 50);
    player02->setPosition(975, 50);

    statusBarPlayer01.setAvatar(player01Avatar);
    statusBarPlayer02.setAvatar(player02Avatar);
    statusBarPlayer01.setPosition(235, 600);
    statusBarPlayer02.setPosition(675, 600);
}

void GameScene::onDraw(const Camera &camera) {
    // 绘制背景蓝天
    draw(camera, sky, (Content::WINDOW_WIDTH - 1400) / 2, (Content::WINDOW_HEIGHT - 800) / 2);
    // 绘制大山
    draw(camera, hills, (Content::WINDOW_WIDTH - 2000) / 2, (Content::WINDOW_HEIGHT - 800) / 2);
    // 绘制地图
    for (int i = 0; i < platforms.size(); i++) {
        platforms[i]->onDraw(camera);
    }
    // 绘制角色
    player01->onDraw(camera);
    player02->onDraw(camera);
    // 绘制子弹
    for (int i = 0; i < bullets.size(); i++) {
        bullets[i]->onDraw(camera);
    }

    //绘制状态条
    statusBarPlayer01.onDraw(camera);
    statusBarPlayer02.onDraw(camera);

    if (isGameOver) {
        draw(camera, winnerBar, winnerBarPos.x, winnerBarPos.y);
        draw(camera, player01->getHp() <= 0 ? player2pWinner:player1pWinner, winnerBarTextPos.x, winnerBarTextPos.y);
    }
}

void GameScene::onUpdate(int delta) {
    player01->onUpdate(delta);
    player02->onUpdate(delta);

    camera.onUpdate(delta);

    // 子弹碰撞后移除子弹
    auto new_end = std::remove_if(bullets.begin(), bullets.end(), [](const Bullet *bullet) {
        return bullet->checkCanRemove();
    });
    bullets.erase(new_end, bullets.end());

    // 更新子弹
    for (int i = 0; i < bullets.size(); i++) {
        bullets[i]->onUpdate(delta);
    }
    // 玩家掉到界面外
    if (player01->getSize().y >= Content::WINDOW_HEIGHT) {
        player01->setHp(0);
    }
    if (player02->getSize().y >= Content::WINDOW_HEIGHT) {
        player02->setHp(0);
    }

    // 更新状态条
    statusBarPlayer01.setHp(player01->getHp());
    statusBarPlayer02.setHp(player02->getHp());
    statusBarPlayer01.setMp(player01->getMp());
    statusBarPlayer02.setMp(player02->getMp());

    // 判断游戏是否结束
    if (player01->getHp() <= 0 || player02->getHp() <= 0) {
        isGameOver = true;
    }

    // 游戏获胜提示栏显示
    if (isGameOver) {
        winnerBarPos.x += (int)winnerBarSpeed * delta;
        winnerBarTextPos.x += (int)winnerBarTextSpeed * delta;
        if (!isSolidOutStarted) { // 当没有划出时
            winnerBarSolidInTimer.onUpdate(delta);
            // 停顿展示
            if (winnerBarPos.x >= winnerBarPosDst.x) {
                winnerBarPos.x = winnerBarPosDst.x;
            }
            if (winnerBarTextPos.x >= winnerBarTextPosDst.x) {
                winnerBarTextPos.x = winnerBarTextPosDst.x;
            }
        }else { // 当开始划出时
            winnerBarSolidOutTimer.onUpdate(delta);
        }
    }
}

void GameScene::onInput(const SDL_Event &e) {
    player01->onInput(e);
    player02->onInput(e);
    if (e.type == SDL_KEYDOWN) {
        // debuge 模式
        switch (e.key.keysym.sym) {
            case SDLK_o:
                IS_DEBUG_MODE = !IS_DEBUG_MODE;
                break;
            default: break;
        }
    }
}

void GameScene::onExit() {
    delete player01;
    player01 = nullptr;
    delete player02;
    player02 = nullptr;
    bullets.clear(); // 清空子弹列表
    camera.reset(); // 重置摄像机的位置
}
